<template>
	<view class="question-content">
		<view class="question-box">
			<u-sticky>
				<u-tabs :list="list" :is-scroll="false" :current="current" @change="change"></u-tabs>
			</u-sticky>
			<view v-if="current == 0">
				<view class="wrong-box">
					<view class="wrong-top">
						<image src="../../static/index/组 2.png" mode=""></image>
						<view class="wrong-num">
							<view>{{totalCount}}</view>
							<view>错题数</view>
						</view>
					</view>
					<view class="wrong-btn">
						<u-button type="success" shape="circle" :custom-style="customStyle" @click="lookDetail('error')">全部错题</u-button>
					</view>
				</view>
				<view class="wrong-list">
					<view class="wrong-item" v-for="(item,index) in listData" :key="index"  @click="lookDetail('error',item.type)">
						<view>{{item.type_name}}</view>
						<view>
							<text>{{item.type_count}}</text>
							<u-icon name="arrow-right"></u-icon>
						</view>
						<view class="clear">
							<u-button type="success" shape="circle" :custom-style="customStyle" @click="delAllwrongTopic">清空全部错题</u-button>
						</view>
					</view>
				</view>
			</view>
			<view v-if="current == 1">
				<view class="wrong-box">
					<view class="wrong-top">
						<image src="../../static/index/组 2.png" mode=""></image>
						<view class="wrong-num">
							<view>{{totalCount}}</view>
							<view>收藏数</view>
						</view>
					</view>
					<view class="wrong-btn">
						<u-button type="success" shape="circle" :custom-style="customStyle" @click="lookDetail('collection')">全部收藏</u-button>
					</view>
				</view>
				<view class="wrong-list">
					<view class="wrong-item" v-for="(item,index) in listData" :key="index" @click="lookDetail('collection',item.type)">
						<view>{{item.type_name}}</view>
						<view>
							<text>{{item.type_count}}</text>
							<u-icon name="arrow-right"></u-icon>
						</view>
					</view>
					<view class="clear">
						<u-button type="success" shape="circle" :custom-style="customStyle" @click="delAllcollection">清空我的收藏</u-button>
					</view>
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	import { getErrType,getCollectionType,delAllcollection,delAllwrongTopic } from '../../api/user.js'
import { toast } from '../../utils/common.js';
	export default {
		data() {
			return {
				customStyle: {
					background: '#2AC092',
					color: '#fff'
				},
				current: 0,
				currentTag:0,
				list: [{
					name: '错题集'
				}, {
					name: '收藏集'
				}],
				listData:[],
				totalCount:0,
				
				//
				subject:1,
			}
		},
		onLoad(options) {
			this.current = options.current;
			this.currentTag = options.currentTag
			if(options.currentTag==4){
				this.subject=2
			}
		},
		mounted() {
			if(this.current == 0){
				this.getErrTypes()
			}
			if(this.current == 1){
				this.getCollectionTypes()
			}
		},
		methods: {
			// 查看收藏/错题
			lookDetail(model,type){
				uni.navigateTo({
					url:'/pagesA/redo-topic/redo-topic?model=' + model + '&type=' + type+'&currentTag='+this.currentTag
				})
			},
			// 清空所有收藏
			delAllcollection(){
				delAllcollection({subject:this.subject}).then(res=>{
					// console.log(888,res.data);
					if(res.data.code = 200){
						toast('已清空')
						this.getCollectionTypes()
					}
				})
			},
			// 删除全部错题
			delAllwrongTopic(){
				delAllwrongTopic({subject:this.subject}).then(res=>{
					if(res.data.code = 200){
						toast('已清空')
						this.getErrTypes()
					}
				})
			},
			change(index) {
				this.current = index;
				if(index == 0){
					this.getErrTypes()
				}
				if(index == 1){
					this.getCollectionTypes()
				}
			},
			getErrTypes(){
				let that = this
				that.totalCount = 0
				getErrType({subject:this.subject}).then(res=>{
					that.listData = res.data.data;
					that.listData.map(item=>{
						// console.log();
						that.totalCount += item.type_count
					})
				})
			},
			getCollectionTypes(){
				let that = this
				that.totalCount = 0
				getCollectionType({subject:this.subject}).then(res=>{
					// this.listData = []
					that.listData = res.data.data;
					that.listData.map(item=>{
						// console.log();
						that.totalCount += item.type_count
					})
				})
			},
		}
	}
</script>

<style lang="scss">
	.question-box {


		.wrong-box {
			padding: 20rpx;
			margin-top: 20rpx;
			background-color: #fff;

			.wrong-top {
				position: relative;

				>image {
					width: 710rpx;
					height: 462rpx;
				}

				.wrong-num {
					position: absolute;
					right: 0;
					top: 42%;
					width: 100%;
					display: flex;
					color: #fff;
					flex-direction: column;
					align-items: center;

					>view:nth-child(1) {
						font-size: 72rpx;
						font-weight: bold;
						margin-bottom: 50rpx;
					}
				}
			}

			.wrong-btn {
				width: 95%;
				margin: 50rpx auto;
			}
		}

		.wrong-list {
			margin-top: 20rpx;
			padding: 0 20rpx;
			background-color: #fff;
			min-height: 40vh;
			padding-bottom: 120rpx;
			position: relative;

			.wrong-item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 40rpx 0;
				border-bottom: 1rpx solid #DFE0E6;
				font-size: 28rpx;

				>view:nth-child(2) {
					color: #999999;
				}
			}

			.clear {
				width: 70%;
				position: absolute;
				left: 15%;
				bottom: 15px;
			}
		}
	}
</style>